﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JCPogi.Core {

    public enum DeviceCommandResponseType {
        Ok = 0,
        MessagePrompt,
        Error,
    }

    public class DeviceCommandResponse {

        public const string ACK_OK = "\r\nOK\r\n";
        public const string PRM_MSG = "\r\n> ";
        public const string ACK_ERR = "\r\nERROR\r\n";
        public const string CMS_ERR500 = "\r\n\r\n+CMS ERROR: 500\r\n";

        public static bool IsAckReceived(string responseBuffer) {
            return responseBuffer.EndsWith(ACK_OK) ||
                   responseBuffer.EndsWith(PRM_MSG) || 
                   responseBuffer.EndsWith(ACK_ERR) ||
                   responseBuffer.EndsWith(CMS_ERR500);
        }

        public DeviceCommandResponse(string buffer) {
            Buffer = buffer;
        }

        public virtual string Buffer { get; private set; }

        public DeviceCommandResponseType Type {
            get {
                DeviceCommandResponseType result = DeviceCommandResponseType.Ok;
                
                if (Buffer.EndsWith(ACK_OK)) result = DeviceCommandResponseType.Ok;
                if (Buffer.EndsWith(PRM_MSG)) result = DeviceCommandResponseType.MessagePrompt;
                if (Buffer.EndsWith(ACK_ERR) || Buffer.EndsWith(CMS_ERR500)) result = DeviceCommandResponseType.Error;

                return result;
            }
        }
    }
}
